package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.UserSession;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.model.IMLogin;
import com.alan.wifishare.model.impl.MLoginImpl;
import com.alan.wifishare.utils.BusinessUtil;
import com.alan.wifishare.utils.Md5Util;
import com.alan.wifishare.view.iview.IVUserLogin;

/**
 * @className: UserLoginPS
 * @describtion: UserLoginPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 15:41
 */
public class UserLoginPS {

    /**
     * M层的登录Model实例
     */
    private IMLogin mLogin;

    /**
     * V层登录View的实例
     */
    private IVUserLogin vLogin;

    public UserLoginPS(IVUserLogin vLogin) {
        mLogin = new MLoginImpl();
        this.vLogin = vLogin;
    }

    /**
     * 执行登录操作
     */
    public void login() {
        String userAccount = vLogin.getUserAccount();
        String userPwd = vLogin.getUserPwd();
        KLog.e("userAccount = " + userAccount);
        KLog.e("userPwd = " + userPwd);
        KLog.e("userPwdMd5 = " + Md5Util.stringToMd5(userPwd));

        if(TextUtils.isEmpty(userAccount)){
            vLogin.toastMsg("账号不能为空");
            return;
        } else if(!BusinessUtil.isMobileNum(userAccount)){
            vLogin.toastMsg("手机号格式错误");
            return;
        }
        if(TextUtils.isEmpty(userPwd)){
            vLogin.toastMsg("密码不能为空");
            return;
        }
        userPwd = Md5Util.stringToMd5(userPwd);
        vLogin.showProgress();
        mLogin.login(userAccount, userPwd, new CallbackListener<UserSession>() {
            @Override
            public void onSuccess(final UserSession result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
                                vLogin.hideProgress();
                                vLogin.loginSuccess(result);
                            }
                        });
                    }
                });
            }
            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
                                vLogin.hideProgress();
                                vLogin.toastMsg(msg);
                            }
                        });
                    }
                });
            }
        });
    }

}
